#include <cstdio>
#include <vector>
constexpr int MaxN = 5e2 + 5;
constexpr long long Mod = 111121;
int n;
int p[MaxN];
std::vector<int> r;
std::vector<int> g[MaxN];
inline long long fsp(long long x, long long y)
{
    long long res = 1;
    for (; y != 0;)
    {
        if (y & 1)
        {
            res *= x;
            res %= Mod;
        }
        x *= x;
        x %= Mod;
        y >>= 1;
    }
    return res;
}
bool road(int u, int f, int t)
{
    r.push_back(u);
    if (u == t)
    {
        return true;
    }
    for (auto v : g[u])
    {
        if (v == f)
        {
            continue;
        }
        if (road(v, u, t))
        {
            return true;
        }
    }
    r.pop_back();
    return false;
}
int main()
{
    freopen("abstract.in", "r", stdin);
    freopen("abstract.out", "w", stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &p[i]);
    }
    for (int i = 1; i <= n - 1; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    long long answer = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = i; j <= n; j++)
        {
            r.clear();
            road(i, 0, j);
            int x, y;
            x = p[*r.begin()];
            y = 0;
            for (auto k : r)
            {
                x &= p[k];
                y |= p[k];
            }
            answer += fsp(x, y);
            answer %= Mod;
        }
    }
    printf("%lld\n", answer);
    return 0;
}